package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.SearchConfigPO;
import com.falcon.domain.dos.SearchConfigDO;
import com.falcon.domain.req.SearchConfigQuery;
import com.falcon.domain.req.SearchConfigRequest;

import com.falcon.mapper.ISearchConfigMapper;
import com.falcon.service.ISearchConfigService;


@Slf4j
@Service
public class SearchConfigService implements ISearchConfigService {

    @Autowired
    private ISearchConfigMapper searchConfigMapper;

    @Override
    public Long save(SearchConfigRequest request){
        SearchConfigPO searchConfigPO = BeanUtil.copyProperties(request, SearchConfigPO.class);

        this.searchConfigMapper.insertNotNull(searchConfigPO);

        return searchConfigPO.getId();
    }

    @Override
    public Boolean update(Long id, SearchConfigRequest request){

        SearchConfigPO searchConfigPO = BeanUtil.copyProperties(request, SearchConfigPO.class);
        searchConfigPO.setId(id);

        return this.searchConfigMapper.updateNotNull(searchConfigPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.searchConfigMapper.deleteById(id);
    }

    @Override
    public SearchConfigDO queryById(Long id){

        SearchConfigPO searchConfigPO = this.searchConfigMapper.selectById(id);

        SearchConfigDO searchConfigDO = BeanUtil.copyProperties(searchConfigPO, SearchConfigDO.class);

        return searchConfigDO;
    }


    @Override
    public List<SearchConfigDO> queryList(SearchConfigQuery request){

        Condition condition = request.toCondition();

        List<SearchConfigPO> poList = this.searchConfigMapper.selectList(condition);

        List<SearchConfigDO> doList = BeanUtil.copyToList(poList, SearchConfigDO.class);

        return doList;
    }

    @Override
    public BasePager<SearchConfigDO> queryPager(SearchConfigQuery request){


        PageInfo<SearchConfigPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.searchConfigMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, SearchConfigDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.searchConfigMapper.deleteByIds(ids);
    }


}
